home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
begincpp
/
#5_ver2.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-04
|
1KB
|
89 lines
#include <stdio.h>
class animal {
public:
virtual void identify() { printf("%s\n", "animal"); }
};
class mammal : public animal {
public:
void identify() { printf("%s\n", "mammal"); }
};
class bird : public animal {
public:
void identify() { printf("%s\n", "bird"); }
};
class dog : public mammal {
public:
void identify() { printf("%s\n", "dog"); }
};
class eagle : public bird {
public:
void identify() { printf("%s\n", "eagle"); }
};
void print1(void), print2(void), print3(void);
main()
{
print1();
print2();
print3();
}
void print1()
{
animal *ap = new animal; ap->identify();
mammal *mp = new mammal; mp->identify();
bird *bp = new bird; bp->identify();
dog *dp = new dog; dp->identify();
eagle *ep = new eagle; ep->identify();
printf("\n");
}
void print2()
{
animal *ap = new animal;
ap->identify();
((mammal *) ap)->identify();
mammal *mp = (mammal *) ap;
mp->identify();
printf("\n");
}
void print3()
{
animal a, *ap = &a;
mammal m;
eagle e;
ap->identify();
ap = &m;
ap->identify();
printf("\n");
}
/* output:
animal
mammal
bird
dog
eagle
animal
animal
animal
animal
mammal
*/